﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;

namespace DBDataContext.Model
{
    [Table]
    public class Manga : INotifyPropertyChanged, INotifyPropertyChanging
    {
        // Define ID: private field, public property, and database column.
        private int _mangaId;
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int MangaId
        {
            get { return _mangaId; }
            set
            {
                if (_mangaId != value)
                {
                    NotifyPropertyChanging("MangaId");
                    _mangaId = value;
                    NotifyPropertyChanged("MangaId");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _name;
        [Column]
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    NotifyPropertyChanging("Name");
                    _name = value;
                    NotifyPropertyChanged("Name");
                }
            }
        }

        // Define item name: private field, public property, and database column.
        private string _folderName;
        [Column]
        public string FolderName
        {
            get { return _folderName; }
            set
            {
                if (_folderName != value)
                {
                    NotifyPropertyChanging("FolderName");
                    _folderName = value;
                    NotifyPropertyChanged("FolderName");
                }
            }
        }

        private Boolean _isAutoDownload;
        [Column]
        public Boolean IsAutoDownload
        {
            get { return _isAutoDownload; }
            set
            {
                if (_isAutoDownload != value)
                {
                    NotifyPropertyChanging("IsAutoDownload");
                    _isAutoDownload = value;
                    NotifyPropertyChanged("IsAutoDownload");
                }
            }
        }

        private string _dataLinkDownload;
        [Column]
        public string DataLinkDownload
        {
            get { return _dataLinkDownload; }
            set
            {
                if (_dataLinkDownload != value)
                {
                    NotifyPropertyChanging("DataLinkDownload");
                    _dataLinkDownload = value;
                    NotifyPropertyChanged("DataLinkDownload");
                }
            }
        }

        private bool _isCompleted;
        [Column]
        public bool IsCompleted
        {
            get { return _isCompleted; }
            set
            {
                if (_isCompleted != value)
                {
                    NotifyPropertyChanging("IsCompleted");
                    _isCompleted = value;
                    NotifyPropertyChanged("IsCompleted");
                }
            }
        }

        private string _imageName;
        [Column]
        public string ImageName
        {
            get { return _imageName; }
            set
            {
                if (_imageName != value)
                {
                    NotifyPropertyChanging("ImageName");
                    _imageName = value;
                    NotifyPropertyChanged("ImageName");
                }
            }
        }


        private string _description;
        [Column]
        public string Description
        {
            get { return _description; }
            set {
                if (_description != value)
                {
                    NotifyPropertyChanging("Description");
                    _description = value;
                    NotifyPropertyChanged("Description");
                }
            }
        }

        //#region BUILD OUT
        // Define the entity set for the collection side of the relationship.
        private EntitySet<Chapter> _chapters;

        [Association(Storage = "_chapters", OtherKey = "_mangaId", ThisKey = "MangaId")]
        public EntitySet<Chapter> Chapters
        {
            get { return this._chapters; }
            set { this._chapters.Assign(value); }
        }


        // Assign handlers for the add and remove operations, respectively.
        public Manga()
        {
            _chapters = new EntitySet<Chapter>(
                new Action<Chapter>(this.attach_Chapter),
                new Action<Chapter>(this.detach_Chapter)
                );
        }

        // Called during an add operation
        private void attach_Chapter(Chapter chapter)
        {
            NotifyPropertyChanging("Chapter");
            //toDo.Category = this;
            chapter.Manga = this;
        }

        // Called during a remove operation
        private void detach_Chapter(Chapter chapter)
        {
            NotifyPropertyChanging("Chapter");
            //toDo.Category = null;
            chapter.Manga = null;
        }

        //#endregion
        #region TEMPLATE from MS
        //// Version column aids update performance.
        //[Column(IsVersion = true)]
        //private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion

        #endregion
    }

    [Table]
    public class Chapter : INotifyPropertyChanged, INotifyPropertyChanging
    {
        // Define ID: private field, public property and database column
        private int _chapterId;
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int ChapterId
        {
            get { return _chapterId; }
            set
            {
                if (_chapterId != value)
                {
                    NotifyPropertyChanging("ChapterId");
                    _chapterId = value;
                    NotifyPropertyChanged("ChapterId");
                }
            }
        }

        // Define item name: private field, public property and database column
        private string _chapterName;
        [Column]
        public string ChapterName
        {
            get { return _chapterName; }
            set
            {
                if (_chapterName != value)
                {
                    NotifyPropertyChanging("ChapterName");
                    _chapterName = value;
                    NotifyPropertyChanged("ChapterName");
                }
            }
        }

        private string _chapterFileName;
        [Column]
        public string ChapterFileName
        {
            get { return _chapterFileName; }
            set
            {
                if (_chapterFileName != value)
                {
                    NotifyPropertyChanging("ChapterFileName");
                    _chapterFileName = value;
                    NotifyPropertyChanged("ChapterFileName");
                }
            }
        }

        private string _chapterFileLink;
        [Column]
        public string ChapterFileLink
        {
            get { return _chapterFileLink; }
            set
            {
                if (_chapterFileLink != value)
                {
                    NotifyPropertyChanging("ChapterFileLink");
                    _chapterFileLink = value;
                    NotifyPropertyChanged("ChapterFileLink");
                }
            }
        }

        private int _isDownloaded ;
        [Column]
        public int IsDownloaded
        {
            get { return _isDownloaded; }
            set
            {
                if (_isDownloaded != value)
                {
                    NotifyPropertyChanging("IsDownloaded");
                    _isDownloaded = value;
                    NotifyPropertyChanged("IsDownloaded");
                }
            }
        }

        private int _currentIndex;
        [Column]
        public int CurrentIndex
        {
            get { return _currentIndex; }
            set
            {
                if (_currentIndex != value)
                {
                    NotifyPropertyChanging("CurrentIndex");
                    _currentIndex = value;
                    NotifyPropertyChanged("CurrentIndex");
                }
            }
        }

        //public Chapter chapter
        //{
        //    set
        //    {
        //        if (value != null)
        //        {
        //            _chapterFileName = value.ChapterFileName;
        //        }
        //    }
        //}

        #region ASSOCIATION
        // Internal column for the associated ToDoCategory ID value
        [Column]
        internal int _mangaId;

        // Entity reference, to identify the ToDoCategory "storage" table
        private EntityRef<Manga> _manga;

        // Association, to describe the relationship between this key and that "storage" table
        [Association(Storage = "_manga", ThisKey = "_mangaId", OtherKey = "MangaId", IsForeignKey = true)]
        public Manga Manga
        {
            get { return _manga.Entity; }
            set
            {
                NotifyPropertyChanging("Manga");
                _manga.Entity = value;

                if (value != null)
                {
                    _mangaId = value.MangaId;
                }

                NotifyPropertyChanging("Manga");
            }
        }
        #endregion
        
        #region TEMPLATE from MS
        // Version column aids update performance.
        [Column(IsVersion = true)]
        private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion

        #endregion
    }


    
    public class MangaForYouDataContext : DataContext
    {
        // Pass the connection string to the base class.
        public MangaForYouDataContext(string connectionString)
            : base(connectionString)
        { }

        // Specify a table for the to-do items.
        public Table<Chapter> Chapters;

        // Specify a table for the categories.
        public Table<Manga> Mangas;

    }
}



